package com.employee.dao;

import com.employee.entity.Account;
import com.employee.util.DBUtil;
import com.employee.util.IdGenerator;
import com.employee.util.ResultSetMapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class AccountDao {
    
    public Account findByUsername(String username) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM t_account WHERE username = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                Account account = new Account();
                account.setId(rs.getString("id"));
                account.setUsername(rs.getString("username"));
                account.setPassword(rs.getString("password"));
                
                java.sql.Timestamp lastLogin = rs.getTimestamp("last_login_time");
                if (lastLogin != null) {
                    account.setLastLoginTime(new Date(lastLogin.getTime()));
                }
                
                java.sql.Timestamp createTime = rs.getTimestamp("create_time");
                if (createTime != null) {
                    account.setCreateTime(new Date(createTime.getTime()));
                }
                
                return account;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        
        return null;
    }
    
    public boolean updateLastLoginTime(String username, Date loginTime) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE t_account SET last_login_time = ? WHERE username = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setTimestamp(1, new java.sql.Timestamp(loginTime.getTime()));
            pstmt.setString(2, username);
            
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, pstmt);
        }
    }
    
    /**
     * 新增账号（演示主键代码生成）
     * 主键通过IdGenerator.generateId()生成，不使用数据库自增
     */
    public boolean insert(Account account) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO t_account (id, username, password) VALUES (?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            
            // 通过代码生成主键ID，不使用数据库自增
            account.setId(IdGenerator.generateId("ADMIN"));
            
            pstmt.setString(1, account.getId());
            pstmt.setString(2, account.getUsername());
            pstmt.setString(3, account.getPassword());
            
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, pstmt);
        }
    }
}
